/*
 * OPIAM Suite
 *
 * Distributable under LGPL license.
 * See terms of license at gnu.org.
 */

package opiam.admin.faare.persistence.javabeans;

import org.apache.commons.beanutils.PropertyUtils;

import java.util.Collection;
import java.util.Iterator;


/**
 * Basic class for business beans.<br>
 * Any mapped object defined in faare_mapping.xml is a subclass of this one.<br>
 * This is the equivalent of the LDAP "top" object class.
 */
public class JBTop implements Cloneable, Comparable
{
    /** Object dname. */
    private String dn;

    /** Last modification timestamp. */
    private String modify;

    /** Creates new JBTop. */
    public JBTop()
    {
    }

    /**
     * This method is called on business bean creation by the persistence layer.<br>
     * It may be overwritten by a subclass to generate computed attributes.
     */
    public void compute()
    {
    }

    /**
     * This method is called by the persistence layer before modifying a business bean object.<br>
     * It may be overwritten by a subclass to generate computed attributes.
     */
    public void decompute()
    {
    }

    /** Gets dname.
     * @return dname.
     */
    public java.lang.String getDn()
    {
        return dn;
    }

    /** Sets dname.
     * @param adn New dname.
     */
    public void setDn(String adn)
    {
        this.dn = adn;
    }

    /** Gets last modification timestamp.
     * @return timestamp.
     */
    public String getModify()
    {
        return modify;
    }

    /** Sets last modification timestamp.
     * @param amodify timestamp
     */
    public void setModify(String amodify)
    {
        this.modify = amodify;
    }

    /**
     * see Comparable.compareTo().
     *
     * @param o see Comparable.compareTo().
     *
     * @return see Comparable.compareTo().
     */
    @Override
	public int compareTo(Object o)
    {
        return 0;
    }

    /**
     * See Object.clone().
     *
     * @return see Object.clone().
     */
    @Override
	public Object clone()
    {
        Object o = null;

        try
        {
            o = super.clone();
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }

        return o;
    }

    /**
     * Checks whether current object is member of the provided object, considering provided attribute.<br>
     * For example, obj will be a group and property the uniqueMember attribute.
     *
     * @param obj including object
     * @param property attribute definining membership
     *
     * @return true or false
     */
    public boolean isMember(Object obj, String property)
    {
        try
        {
            Collection collection = (Collection) PropertyUtils.getProperty(obj,
                    property);

            if (collection != null)
            {
                Iterator it = collection.iterator();
                JBTop entry;

                while (it.hasNext())
                {
                    entry = (JBTop) it.next();
                    System.out.println("isMember entry.getDn() : " +
                        entry.getDn());

                    if (this.dn.equals(entry.getDn()))
                    {
                        return true;
                    }
                }
            }
            else
            {
                return false;
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }

        return false;
    }
}
